<?php
/**
 * Created by JetBrains NetBeans.
 * User: yinyancai
 * Date: 13-3-14 下午4:44
 * To change this template use File | Settings | File Templates.
 */
if (!defined('IN_MOBILE_API')) {
    exit('Access Denied');
}
include_once 'forum.php';
class cloud_api {
    function common() { }

    function output() {
        //判断是否缺少参数
        if (empty($_GET['cpid'])) {
            $variable = array(
                'code' => 1001,
                'messagestr' => lang('plugin/cloud', '1001'),
            );
            cloud_core::result(cloud_core::variable($variable));
        }
        $product = C::t('#cloud#cloud_product')->fetch_info($_GET['cpid']);
        // 更新产品分类 1 删除分类 2 添加分类
        if (!empty($_GET['category'])) {
            C::t('#cloud#cloud_product_category')->delete_by_where('cpid', $_GET['cpid']);
            C::t('#cloud#cloud_product_category')->insert(array('ccid' => $_GET['category'], 'cpid' => $_GET['cpid']));
        }

        //更新产品
        $data = array(
            'cadid' => $_GET['cadid'],
            'name' => $_GET['name'],
            'explain' => $_GET['explain'],
            'material' => empty($_GET['material']) ? $product['material'] : $_GET['material'],
            'term' => empty($_GET['term']) ? $product['term'] : $_GET['term'],
            'isviewbate' => $_GET['isviewbate']==null? $product['isviewbate'] : $_GET['isviewbate'],
            'rebate' =>$_GET['rebate']==null ? $product['rebate'] : $_GET['rebate'],
            'disabled' => $_GET['disabled']==null ? $product['disabled'] : $_GET['disabled'],
            'lasttime' => TIMESTAMP,
            'grade' => empty($_GET['grade']) ? $product['grade'] : $_GET['grade'],
            'recommend' => empty($_GET['recommend']) ? $product['recommend'] : $_GET['recommend'],
            'tel' => empty($_GET['tel']) ? $product['tel'] : $_GET['tel'],
            'address' => empty($_GET['address']) ? $product['address'] : $_GET['address'],
            'longitude' => empty($_GET['longitude']) ? $product['longitude'] : $_GET['longitude'],
            'latitude' => empty($_GET['latitude']) ? $product['latitude'] : $_GET['latitude'],
            'weight'=>empty($_GET['weight']) ?  $product['weight'] : intval($_GET['weight']),
        );
        $result = C::t('#cloud#cloud_product')->update($_GET['cpid'], $data);


        //1>删除规格分类
        C::t('#cloud#cloud_product_standard')->delete_product_id($_GET['cpid']);
        // 2>添加规格分类
        $csid_array = explode(",", $_GET['csid_array']);
        $price_array = explode(",", $_GET['price_array']);
        foreach ($csid_array as $k => $v) {
            $product_standard_data = array(
                'csid' => $v,
                'cpid' =>$_GET['cpid'],
                'price' => $price_array[$k],
                'reprice' => $price_array[$k] * ($data['rebate'] / 10),
                'stock' => $_GET['stock'],
            );
            C::t('#cloud#cloud_product_standard')->insert($product_standard_data);
        }
        $product_category = C::t('#cloud#cloud_product_category')->fetch_info_ccid($_GET['cpid']);
        //更新分类
        $category_data = array('cpid' => $_GET['cpid'], 'ccid' => $_GET['category']);
        C::t('#cloud#cloud_product_category')->update($product_category['cpcid'], $category_data);


        //如果更新失败，这返回错误信息
        if (empty($result)) {
            $variable = array(
                'code' => 11004,
                'messagestr' => lang('plugin/cloud', '11004'),
            );
            cloud_core::result(cloud_core::variable($variable));
        }

        //正常返回
        $variable = array(
            'data' => array(
                'messagestr' => lang('plugin/cloud', '11005'),
                'id' => $_GET['cpid'],
            )
        );
        cloud_core::result(cloud_core::variable($variable));
    }
}

?>

